import tree from "@/components/currencyManage/el-trees"; //左侧机构树
import TableData from "@/components/public/el-tables";
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import {
  getXmCompanyTreeList, //树状图接口
  htsaveStationWarn, // 查询-拌合站预警信息-详情接口
  htgetStationWarnByCompanyId, // 查询-拌合站预警信息-新增接口
} from "@/api/request.js";
import { statusType4 } from "@/utils/config"; //配置项
export default {
  name: "htOverSeting",
  data() {
    return {
      name: "htOverSeting", //按钮配置页面代号
      nature: null,
      yj_save: null,
      formData: {}, //新增编辑向后台传递对象
      // 拌和站列表配置项
      table_config: {
        classCell: true,
        thead: [
          {
            label: "",
            prop: "name",
            minWidth: "35px",
            filter: true,
            btn: true,
            align: "center",
            colorName: "",
            callback: (rowData) => {
              let data = statusType4[rowData.name];
              if (data) {
                return data.label;
              }
            },
            specialClass: (rowData) => {
              let data = statusType4[rowData.name];
              if (data) {
                return data.className;
              }
            },
          },
          {
            label: "合格率",
            prop: "examine",
            minWidth: "60px",
            type: "number",
            disabled: false, //是否禁用
            align: "center",
            edit: true,
          },
          {
            label: "闭合流程",
            prop: "examine0",
            minWidth: "60px",
            type: "option",
            disabled: false, //是否禁用
            option: true,
            align: "center",
            options: [
              // {
              //   value: "无需闭合",
              //   label: "无需闭合",
              // },
              {
                value: "一级流程",
                label: "一级流程",
              },
              {
                value: "二级流程",
                label: "二级流程",
              },
              {
                value: "三级流程",
                label: "三级流程",
              },
            ],
          },
          {
            label: "一级审核",
            prop: "examine1",
            minWidth: "60px",
            type: "option",
            disabled: false, //是否禁用
            option: true,
            options: [],
            align: "center",
          },
          {
            label: "二级审核",
            prop: "examine2",
            minWidth: "60px",
            type: "option",
            disabled: false, //是否禁用
            option: true,
            options: [],
            align: "center",
          },
          {
            label: "三级审核",
            prop: "examine3",
            minWidth: "60px",
            type: "option",
            disabled: false, //是否禁用
            option: true,
            options: [],
            align: "center",
          },
        ],
        isCheckBox: false,
        isIndex: false,
        isOperation: true,
      },
      tableData: [], //表格列表数据列表
      fAduitUser1List: [], //一级审核拼接
      fAduitUser2List: [], //一级审核拼接
      fAduitUser3List: [], //一级审核拼接
      detailData: {}, //拌合站预警信息-详情
      highlightBd: true, //左侧机构树停留背景色
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级
      showName: "未选择公司",
      dialogHeight: "330px", //新增编辑弹框高度
      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      // 列表给后端传参
      getList: {
        fCompanyid: "", // 施工标段id
      },
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据
      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      userInfo: {}, //登录用户
      checkData: [],
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
      fNatureList: null, //机构性质
      jgxzList: {
        value: "",
        label: "",
      }, //机构性质赋值
    };
  },
  components: {
    tree, //左侧组织机构数
    TableData, //主页面列表
    elBreadcrumb, //面包屑
    // publicForm,//弹框表格
  },
  created() {
    // 获取机构性质  一级审核至五级审核下拉框内容
    this.fNatureList = JSON.parse(
      window.sessionStorage.getItem("CompanyNature")
    );
    let listOption = [
      {
        value: "==请选择==",
        label: "==请选择==",
      },
    ];
    this.jgxzList = {
      value: "",
      label: "",
    };
    for (let index = 0; index < this.fNatureList.length; index++) {
      const element = this.fNatureList[index];
      this.jgxzList = {
        value: element.fItemValue,
        label: element.fItemName,
      };
      listOption.push(this.jgxzList);
    }

    for (let index = 3; index < this.table_config.thead.length; index++) {
      const element = this.table_config.thead[index];
      element.options = listOption;
    }

    // 获取机构性质  一级审核至五级审核下拉框内容
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    if (
      JSON.parse(sessionStorage.getItem("materialDataManage_treeCheck"))
    ) {
      var id = JSON.parse(
        sessionStorage.getItem("materialDataManage_treeCheck")
      );
      this.checkData = [id.fCompanyid];
    }
    this.getTree();
    this.btnRole(this.name);
  },
  methods: {
    btnRole(name) {
      // 按钮权限
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        this.yj_save = btnStr.includes("yj_save");
      } else {
        this.yj_save = true;
      }
    },
    // 点击列表当前行，获取当前信息
    rowChange(row) {
      this.fId = row.fId;
    },
    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getXmCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);

            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }
            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("materialDataManage_treeCheck")
              ).fShortname;

              this.getList.fCompanyid = this.checkData[0];
            } else {
              this.checkData[0] = this.treeData[0].fCompanyid;

              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
            }

            // if (this.treeData[0].fNature == '建设单位') {

            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // }
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    // 点击左侧机构树
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      this.getList.fCompanyid = data.fCompanyid;
      this.nature = data.fNature;

      // if (data.fNature == '建设单位') {
      this.yj_searchForm(this.getList);
      // } else {
      //     this.tableData = []
      // }
    },

    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      htgetStationWarnByCompanyId(data)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.listLoading = false;
            var list = res.data.yjQmpMsCheckEntitywarn;
            this.formData = list;
            this.detailData = res.data.yjQmpMsCheckEntitywarn;
            var newfAduitUser1List = list.fAduitUser1.split("-");
            var newfAduitUser2List = list.fAduitUser2.split("-");
            this.fAduitUser1List = [
              newfAduitUser1List[0],
              newfAduitUser1List[1],
              newfAduitUser1List[2],
            ];
            this.fAduitUser2List = [
              newfAduitUser2List[0],
              newfAduitUser2List[1],
              newfAduitUser2List[2],
              // newfAduitUser2List[3],
              // newfAduitUser2List[4],
            ];

            this.tableData = [
              {
                name: "1",
                examine: list.fAdmixture1, //合格率
                examine0: list.fExamine1, //闭合流程
                examine1: this.fAduitUser1List[0], //一级审核
                examine2: this.fAduitUser1List[1], //二级审核
                examine3: this.fAduitUser1List[2], //三级审核
              },
              {
                name: "2",
                examine: list.fAdmixture2, //合格率
                examine0: list.fExamine2, //闭合流程
                examine1: this.fAduitUser2List[0], //一级审核
                examine2: this.fAduitUser2List[1], //二级审核
                examine3: this.fAduitUser2List[2], //三级审核
              },
            ];
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 保存接口
    yj_saveForm() {
      htsaveStationWarn(this.formData)
        .then((res) => {
          if (res.errcode == 0) {
            this.yj_searchForm(this.getList);
            this.$message.success("保存成功");
          } else {
            this.$message.error(res.msg);
            console.log("接口报错");
          }
        })
        .catch((error) => {
          console.log("报警参数设置-新增）", error);
        });
    },
    // 关闭新增编辑弹框
    resetForm() {
      this.dialogEditVisible = false;
    },
    // 详情
    inputValue(val, table) {
      // console.log("inputValue", val, table);
      for (let index = 0; index < table.length; index++) {
        const element = table[index];
        if (element.examine1 == "==请选择==") {
          element.examine1 = "";
        } else if (element.examine2 == "==请选择==") {
          element.examine2 = "";
        } else if (element.examine3 == "==请选择==") {
          element.examine3 = "";
        }
      }
      this.formData = {
        fId: this.detailData.fId, // 主键id
        fCompanyid: this.getList.fCompanyid, // 施工单位id
        fCompanyname: this.showName, //施工单位

        fAdmixture1: Number(table[0].examine), // 合格率--回弹
        fAdmixture2: Number(table[1].examine), // 合格率--钢保

        fExamine1: table[0].examine0, // 闭合流程 低预警
        fExamine2: table[1].examine0, // 闭合流程 中预警

        fAduitUser1:
          table[0].examine1 + "-" + table[0].examine2 + "-" + table[0].examine3, // 审核流程 一級预警
        fAduitUser2:
          table[1].examine1 + "-" + table[1].examine2 + "-" + table[1].examine3, // 审核流程 二級预警
      };
    },

    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};

